package com.ticket.web.controller.yanchu;

import com.ticket.common.annotation.Log;
import com.ticket.common.core.controller.BaseController;
import com.ticket.common.core.domain.AjaxResult;
import com.ticket.common.core.page.TableDataInfo;
import com.ticket.common.enums.BusinessType;
import com.ticket.common.exception.base.BaseException;
import com.ticket.common.utils.Const;
import com.ticket.common.utils.poi.ExcelUtil;
import com.ticket.system.domain.TYanchuPosters;
import com.ticket.system.service.ITYanchuPostersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 海报管理Controller
 * 
 * @author ruoyi
 * @date 2023-09-27
 */
@Api(description = "海报管理")
@RestController
@RequestMapping("/system/posters")
public class TYanchuPostersController extends BaseController
{
    @Autowired
    private ITYanchuPostersService tYanchuPostersService;


    @ApiOperation("查询海报管理列表")
    @PreAuthorize("@ss.hasPermi('system:posters:list')")
    @GetMapping("/list")
    public TableDataInfo list(TYanchuPosters tYanchuPosters)
    {
        startPage();
        List<TYanchuPosters> list = tYanchuPostersService.selectTYanchuPostersList(tYanchuPosters);
        return getDataTable(list);
    }

    @ApiOperation("小程序查询海报管理列表")
    @GetMapping("/list/forAllUser")
    public TableDataInfo listForAllUser(TYanchuPosters tYanchuPosters)
    {
        startPage();
        List<TYanchuPosters> list = tYanchuPostersService.selectTYanchuPostersList(tYanchuPosters);
        return getDataTable(list);
    }

    /**
     * 导出海报管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:posters:export')")
    @Log(title = "海报管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TYanchuPosters tYanchuPosters)
    {
        List<TYanchuPosters> list = tYanchuPostersService.selectTYanchuPostersList(tYanchuPosters);
        ExcelUtil<TYanchuPosters> util = new ExcelUtil<TYanchuPosters>(TYanchuPosters.class);
        util.exportExcel(response, list, "海报管理数据");
    }

    @ApiOperation("获取海报管理详细信息")
    @ApiImplicitParam(name = "id",value = "主键id",required = true,dataType = "Long",dataTypeClass = Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tYanchuPostersService.selectTYanchuPostersById(id));
    }


    @ApiOperation("新增海报管理")
    @ApiImplicitParam(name = "tYanchuPosters",value = "海报管理实体",dataType = "TYanchuPosters",dataTypeClass = TYanchuPosters.class)
    @PreAuthorize("@ss.hasPermi('system:posters:add')")
    @Log(title = "海报管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TYanchuPosters tYanchuPosters)
    {
        Assert.notNull(tYanchuPosters.getPostersName(),"海报名称不能为空");
        Assert.notNull(tYanchuPosters.getPostersUrl(),"海报封面地址不能为空");
        Assert.notNull(tYanchuPosters.getActType(),"链接地址类型不能为空");
        if (Const.TWO.equals(tYanchuPosters.getActType())){
            //站内链接
            if (null == tYanchuPosters.getOperaId() || null == tYanchuPosters.getMovieId()){
                throw new BaseException("站内链接,剧院和演出不能为空");
            }
        }
        if (Const.THREE.equals(tYanchuPosters.getActType())){
            //站外链接
            if (StringUtils.isEmpty(tYanchuPosters.getOffSiteUrl())){
                throw new BaseException("站外链接地址不能为空");
            }
        }
        //校验海报名称是否重复
        if (tYanchuPostersService.hasPosterName(tYanchuPosters.getPostersName())){
            throw new BaseException("名称已存在");
        }
        return toAjax(tYanchuPostersService.insertTYanchuPosters(tYanchuPosters));
    }


    @ApiOperation("修改海报管理")
    @ApiImplicitParam(name = "tYanchuPosters",value = "海报管理实体",dataType = "TYanchuPosters",dataTypeClass = TYanchuPosters.class)
    @PreAuthorize("@ss.hasPermi('system:posters:edit')")
    @Log(title = "海报管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody TYanchuPosters tYanchuPosters)
    {
        Assert.notNull(tYanchuPosters.getId(),"关键字段缺失");
        if (StringUtils.isNotEmpty(tYanchuPosters.getPostersName())){
            //校验名称是否重复
            TYanchuPosters posters = tYanchuPostersService.selectTYanchuPostersById(tYanchuPosters.getId());
            if (!posters.getPostersName().equals(tYanchuPosters.getPostersName())){
                if (tYanchuPostersService.hasPosterName(tYanchuPosters.getPostersName())){
                    throw new BaseException("当前海报名称已存在,请重新设置");
                }
            }
        }
        return toAjax(tYanchuPostersService.updateTYanchuPosters(tYanchuPosters));
    }


    @ApiOperation("删除海报管理")
    @ApiImplicitParam(name = "ids",value = "主键 多个逗号拼接",dataType = "String",required = true,dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermi('system:posters:remove')")
    @Log(title = "海报管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String ids)
    {
        List<Long> collect = Arrays.stream(ids.split(",")).map(s -> Long.valueOf(s.trim())).collect(Collectors.toList());
        return toAjax(tYanchuPostersService.deleteTYanchuPostersByIds(collect));
    }

    @ApiOperation("上移下移")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "主键id",required = true,dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "type",value = "1:上移;2:下移",required = true,dataType = "long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "orderNum",value = "排序值",required = true,dataType = "int",dataTypeClass = Integer.class)
    })
    @Log(title = "海报管理", businessType = BusinessType.UPDATE)
    @PutMapping("/upOrDown")
    public AjaxResult upOrDown(@RequestParam Long id,@RequestParam Integer type,@RequestParam Integer orderNum){
        return toAjax(tYanchuPostersService.upOrDown(id,type,orderNum));
    }

    @ApiOperation("修改海报状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "主键",required = true,dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "status",value = "海报状态 1：开启；0：关闭",required = true,dataType = "int",dataTypeClass = Integer.class)
    })
    @GetMapping("/statueOpenOrClose")
    public AjaxResult statueOpenOrClose(Long id,Integer status){
        return toAjax(tYanchuPostersService.statueOpenOrClose(id,status));
    }
}
